



const state = {
	//会话列表
	conversations: []
}

const actions = {
	// 获取聊天信息列表以及最后的信息
	getMessagesAction({commit, state}, message){
		commit('getMessage', message)
	},
	// 设置单聊数据在会话列表数据更新
	/**
	 * data : {conversation: [],// 会话列表 data: [] // 消息}
	 */
	updateConversationsAction({commit, state},data) {
		commit('updateConversations', data)
	},
	
	// 获取极光话列表数据
	getJMConversationsAction({commit, state}, message) {
		commit('getJMConversations', message)
	},
	
	// 发送消息更新会话列表
	sendMsgUpdateConversationAction({commit, state}, content) {
		commit('sendMsgUpdateConversation', content)
	}
}

const mutations = {
	getMessage(state, data) {
		
		state.conversations = data.filter(item => {
			return !(item.extras && item.extras.type === 'remove')
		})
		
	},
	updateConversations(state, data) {
		const conversation = data.conversation;
		const message = data.message;
		let conversations = [];
		const conversationsStore = state.conversations;
		if(message.length ===0) {
			for(let j = 0; j < conversation.length; j++) {
				const conversationItem = conversation[j];
				const index = conversationsStore.findIndex(c => c.username === conversationItem.username)
				if(index > -1) {
					conversations.push({
						...conversationsStore[index],
						unread_msg_count: conversationItem.unread_msg_count,
						time:  conversationItem.time,
						mtime:  conversationItem.mtime,
					})
				} else {
					conversations.push(conversationItem)
				}
			}
		} else {
			for(let i =0; i< message.length; i++) {
				const msgItem = message[i];
				
				for(let j = 0; j < conversation.length; j++) {
					const conversationItem = conversation[j];
					const index = conversationsStore.findIndex(c => c.username === conversationItem.username)
					
					if(conversationItem.username === msgItem.from_username) {
						conversations.push({
							...conversationItem,
							msg_text: msgItem.content.msg_type === 'image' ?  "[图片]" : msgItem.content.msg_body.text,
						})
					} else {
						if(index > -1) {
							conversations.push({
								...conversationsStore[index],
								unread_msg_count: conversationItem.unread_msg_count,
								time:  conversationItem.time,
								mtime:  conversationItem.mtime,
							})
						} else {
							conversations.push(conversationItem)
						}
					}
				}
			}
		}
		state.conversations = conversations.filter(item => {
			return !(item.extras && item.extras.type === 'remove')
		})
	},
	getJMConversations(state, data = []) {
		const conversations = state.conversations;
		const neewData = data.map(item => {
			const index = conversations.findIndex(c => c.username === item.username)
			if(index> -1) {
				return {
					...conversations[index],
					unread_msg_count: item.unread_msg_count,
					time:  item.time,
					mtime:  item.mtime,
				}
			} else {
				return item
			}
		})
		state.conversations = neewData.filter(item => {
			return !(item.extras && item.extras.type === 'remove')
		})
	},
	
	sendMsgUpdateConversation(state, data) {
		let conversations = state.conversations;
		const msg_type = data.msg_type;
		const index = conversations.findIndex(c => c.username === data.target_name)
	
		if(index > -1) {
			// 存在当中的会话列表中
			conversations[index].unread_msg_count = 0;
			conversations[index].time = data.time;
			conversations[index].mtime = data.content.create_time;
			conversations[index].msg_text = msg_type  === 'image' ?  "[图片]" : data.text;
			
		} else {
			// 不存在会话列表中
			conversations.push({
				unread_msg_count: 0,
				time: data.time,
				mtime: data.content.create_time,
				msg_text:  msg_type  === 'image' ?  "[图片]" : data.text,
				avatar: data.avatar,
				username: data.target_name,
				name: data.target_name,
				nickName: data.content.target_name,
				appkey: data.appkey,
				type: 3,
				extras: {}
			})
		}
		state.conversations = conversations.filter(item => {
			return !(item.extras && item.extras.type === 'remove')
		})
	},
}
/**
 * @param {Object} exit_time
 * 转换日期
 */
function getMathTime(exit_time){
	let _this = this;
	var date = new Date(exit_time);
	var Y = date.getFullYear() + '-';
	var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
	var D = (date.getDate() < 10 ? '0' + (date.getDate()) : date.getDate()) + ' ';
	var h = (date.getHours() < 10 ? '0' + (date.getHours()) : date.getHours()) + ':';
	var m = (date.getMinutes() < 10 ? '0' + (date.getMinutes()) : date.getMinutes()) + '';
	var s = (date.getSeconds() < 10 ? '0' + (date.getSeconds()) : date.getSeconds());
	return M + D
	// return Y+M+D+h+m+':'+s;
}


export default {
	mutations,
	state,
	actions
}